package com.eternal.permission.controller;


import com.eternal.Exception.ResultEnum;
import com.eternal.Exception.ResultVo;
import com.eternal.permission.entity.PermissionInfo;
import com.eternal.permission.service.RolePermissionService;
import com.eternal.util.tree.entity.Tree;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 角色权限表 前端控制器
 * </p>
 *
 * @author Eternal
 * @since 2018-08-26
 */
@RestController
@RequestMapping("/rolePermission")
@Log
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * <p> 获取角色权限树  </p>
     *
     * @Description: TODO
     * @Param
     * @Return
     * @Author Eternal
     * @Date 2018/9/1 23:39
     */
    @GetMapping("/queryPermissionInfoTree")
    @RequiresPermissions("system_permission_role_query_queryPermissionInfoTree")
    public ResultVo<List<Tree<PermissionInfo>>> queryPermissionInfoTree(@RequestParam(required = true) String roleId) {
        List<Tree<PermissionInfo>> permissinon = rolePermissionService.getPermission(roleId);
        return new ResultVo<>(permissinon);
    }

    @GetMapping("/queryPermissionByRoleIds")
    @RequiresPermissions("queryPermissionByRoleIds")
    public ResultVo<Set<PermissionInfo>> queryPermissionByRoleIds(String roles) {
        String[] roless = roles.split(",");
        List<String> list = Arrays.asList(roless);
        Set<PermissionInfo> permissionByRoleIds = rolePermissionService.getPermissionByRoleIds(Arrays.asList(roless));
        return new ResultVo(permissionByRoleIds);

    }

    @PostMapping("/addPermission")
    @RequiresPermissions("system_permission_role_addPermission")
    public ResultVo<ResultEnum> addPermission( String roleId, String permission) {
        ResultEnum result = rolePermissionService.addPermission(roleId, permission.split(",")) ? ResultEnum.ADD_PERMISSION : ResultEnum.ADD_PERMISSION_ERROR;

        return new ResultVo(result);
    }

}

